/*
 * Copyright 2016-2017 lei.xu<xulei2008xulei@163.com>.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.san21go.jfinfunc.time;

import java.util.Date;

import org.joda.time.DateTime;
import org.san21go.jfinfunc.AbstractFunction;
import org.san21go.jfinfunc.FinFuncRuntimeException;

/**
 * DATE函数返回表示特定的日期序列号。<br/>
 * 语法︰DATE(year,month,day)<br/>
 * DATE 函数语法具有下列参数：<br/>
 * 
 * 
 * @author xulei
 *
 */
public class DATE extends AbstractFunction<Date> {

	private int year;
	private int month;
	private int day;

	public DATE(int year, int month, int day) {
		super();
		this.year = year;
		this.month = month;
		this.day = day;
	}

	@Override
	public Date evaluate() throws FinFuncRuntimeException {
		if (year < 0 || year > 10000) {
			throw new FinFuncRuntimeException("year 小于 0 或大于等于 10000，不受支持");
		}

		if (year >= 0 && year <= 1899) {
			year += 1900;
		}

		DateTime dt = new DateTime(year, 1, 1, 0, 0, 0);

		dt = dt.plusMonths(month - 1).plusDays(day - 1);

		return dt.toDate();
	}

}
